<template>
	<view class="my">
		<view class="header">
			<view class="seting" @click="router('/pages/my/seting')">
				<image src="@/static/images/m1.png"></image>
			</view>
			<view class="user_info">
				<view class="avatar">
					<image :src="user_info.head_portrait"></image>
					<view class="label">
						<image src="@/static/images/m2.png"></image>
						<text>{{user_info.level_name}}</text>
					</view>
				</view>
				<view class="user_view">
					<view class="name">{{user_info.nickname}}</view>
					<view class="user_id">ID:{{user_info.id}}</view>
				</view>
				<view class="interest">
					<view class="label" @click="interest">关联公众号</view>
					<!-- <view class="bind_info" v-if="parent">
						<view>负责人:{{parent.nickname}}</view>
						<view>ID:{{parent.id}}</view>
					</view>
					<view class="label" @click="bind_user" v-else>绑定上级</view> -->
					<view class="bind_info">
						<view>负责人:张三13555554444</view>
						<view>ID:730</view>
					</view>
				</view>
			</view>
			<view class="storeroom">
				<view class="store_view">
					<view class="store_head">
						<view class="left">
							<image src="@/static/images/m4.png"></image>
							<text>我的仓库</text>
						</view>
						<view class="right" @click="router('/pages/my/my_cabinet')">
							<text>查看详情</text>
							<image src="@/static/images/m5.png"></image>
						</view>
					</view>
					<!-- <view class="store_item" v-for="item in data_list"
						@click="router(`/pages/cabinet/goods_deta?id=${item.id}`)">
						<view class="left">
							<image src="@/static/images/m16.png"></image>
							<text>{{item.product.name}}</text>
						</view>
						<view class="right">
							<view class="key">数量：</view>
							<view class="value">{{item.stock}}</view>
						</view>
					</view> -->
				</view>
			</view>
		</view>
		<view class="content">
			<uv-notice-bar :text="`你有${invest_value}笔待支付的投资订单`" color="#fff" bgColor="#CA1C1D"
				@click="router('/pages/my/invest_list?type=0')" v-if="invest_value>0" />
			<view class="order_list">
				<view class="order_head">
					<view class="key">我的订单</view>
					<view class="value" @click="router('/pages/my/all_order')">
						<view>全部订单</view>
						<image src="@/static/images/m5.png"></image>
					</view>
				</view>
				<view class="order_cell">
					<view class="item" @click="router('/pages/my/all_order?type=0')">
						<image src="@/static/images/m7.png"></image>
						<text>待付款</text>
					</view>
					<view class="item" @click="router('/pages/my/all_order?type=1')">
						<image src="@/static/images/m9.png"></image>
						<text>待发货</text>
					</view>
					<view class="item" @click="router('/pages/my/all_order?type=2')">
						<image src="@/static/images/m8.png"></image>
						<text>待收货</text>
					</view>
					<view class="item">
						<image src="@/static/images/m10.png"></image>
						<text>退换/售后</text>
					</view>
				</view>
			</view>
			<view class="property">
				<view class="item">
					<view class="cell" @click="router('/pages/my/wallet')">{{user_info.balance}}</view>
					<view class="unit">余额</view>
				</view>
				<view class="item" @click="router('/pages/my/integral')">
					<view class="cell">{{user_info.integral}}</view>
					<view class="unit">积分</view>
				</view>
				<!-- <view class="item" @click="router('/pages/my/coupon')"> -->
				<view class="item" @click="router('/pages/my/coupons')">
					<view class="cell">{{user_info.shopping}}</view>
					<view class="unit">消费券</view>
				</view>
				<view class="item" @click="router('/pages/my/wallet')">
					<view class="cell">
						<image src="@/static/images/m17.png"></image>
					</view>
					<view class="unit">我的钱包</view>
				</view>
			</view>
			<view class="list">
				<view class="item" v-for="item in list" @click="navigate(item)">
					<view class="left">
						<image :src="item.icon"></image>
						<text>{{item.text}}</text>
					</view>
					<image src="@/static/images/r2.png"></image>
				</view>
				<view class="item" @click="scanQRCode" v-if="is_service==1">
					<view class="left">
						<image src="@/static/images/m14.png"></image>
						<text>核销服务订单</text>
					</view>
					<image src="@/static/images/r2.png"></image>
				</view>
			</view>
		</view>
		<uv-popup ref="popupRef" bgColor="none" safeAreaInsetBottom mode="center">
			<Invite :poster="poster" :link="link" @close="popupRef.close" />
		</uv-popup>
		<uv-popup ref="bindRef" bgColor="none" safeAreaInsetBottom mode="center">
			<BindUser @close="bindRefClose" />
		</uv-popup>
	</view>
</template>

<script setup>
	import {
		userInfo,
		orderWriteoff
	} from "@/request/api";
	import Invite from "@/components/my/Invite.vue";
	import BindUser from "@/components/my/BindUser.vue";

	import useWechat from "@/hooks/useWechat.js";
	import useBase from "@/hooks/useBase.js";
	import useShare from "@/hooks/useShare.js";
	import useLogin from "@/hooks/useLogin.js";
	const {
		wxShare,
		wxScanQRCode
	} = useWechat();
	const {
		router
	} = useBase();
	const {
		shop_share,
		home_url
	} = useShare();
	const {
		wxLogin
	} = useLogin();
	import {
		onShow,
		onHide
	} from "@dcloudio/uni-app";
	import {
		ref,
		shallowRef
	} from "vue";
	const list = ref([{
		text: "我的团队",
		icon: new URL("../../static/images/m11.png", import.meta.url).href,
		url: "/pages/my/team"
	}, {
		text: "邀请好友",
		icon: new URL("../../static/images/m12.png", import.meta.url).href,
		popup: true
	}, {
		text: "地址管理",
		icon: new URL("../../static/images/m13.png", import.meta.url).href,
		url: "/pages/my/address"
	}, {
		text: "绑定区域",
		icon: new URL("../../static/images/m14.png", import.meta.url).href,
		url: "/pages/my/bind"
	}, {
		text: "实名认证",
		icon: new URL("../../static/images/m14.png", import.meta.url).href,
		url: "/pages/my/real"
	}]);
	// user_info.value.is_real ? "/pages/my/real_two" : 

	const popupRef = shallowRef();
	const navigate = (item) => {
		let popup = item.popup,
			url = item.url;
		if (!url && !popup) {
			uni.$uv.toast("敬请期待");
			return false;
		}
		url ? router(url) : popupRef.value.open();
	};
	const scanQRCode = () => {
		wxScanQRCode(async (value) => {
			await orderWriteoff({
				order_no: value
			})
			uni.$uv.toast("操作成功");
		});
	};
	const interest = () => {
		window.top.location.href = "https://mp.weixin.qq.com/s/irvSly5P8_oeoLvJ2UlOrg";
	};

	const bindRef = shallowRef();
	const bind_user = () => bindRef.value.open();
	const bindRefClose = () => {
		getUserInfo();
		bindRef.value.close();
	};
	const data_list = ref([]);
	const user_info = ref({
		nickname: "",
		head_portrait: "",
		id: "",
		balance: "",
		integral: "",
		shopping: ""
	});
	const link = ref("");
	const poster = ref("");
	const parent = ref({});
	const invest_value = ref(0);
	const is_service = ref(0);

	const getUserInfo = async () => {
		let {
			invest_count,
			users_underwrite,
			user,
			setting
		} = await userInfo();
		data_list.value = users_underwrite;
		user_info.value = user;
		let invitation = user.invitation_code;
		link.value = `${home_url}?invitation=${invitation}`;
		poster.value = setting.share_poster;
		if (user.is_real == 1) {
			list.value.at(-1).url = "/pages/my/real_show";
		}

		parent.value = user.parent;
		invest_value.value = invest_count;
		is_service.value = user.is_service;

		wxShare({
			title: '小酒馆',
			desc: shop_share.desc,
			link: link.value,
			imgUrl: shop_share.imgUrl
		});

		if (user.province_id == 0) {
			router('/pages/my/bind')
		}
	};
	onShow(() => getUserInfo());
	onHide(() => popupRef.value.close());
</script>

<style scoped lang="scss">
	.my {
		min-height: calc(100vh - var(--window-top) - var(--window-bottom));
		background: $background;

		.header {
			width: 100%;
			padding: 68rpx 30rpx 0;
			box-sizing: border-box;
			background: url(@/static/images/bg1.png) no-repeat;
			background-size: 100% 473rpx;
			position: relative;

			.seting {
				position: absolute;
				right: 44rpx;
				top: 20rpx;
				z-index: 2;

				image {
					width: 50rpx;
					height: 50rpx;
				}
			}

			.user_info {
				display: flex;

				.avatar {
					position: relative;

					image {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
					}

					.label {
						width: 140rpx;
						height: 36rpx;
						border-radius: 18rpx;
						border: 1px solid rgba(134, 138, 152, 0.5);
						background: linear-gradient(-90deg, #E4E4E4 0%, #BCBCBC 100%);
						display: flex;
						justify-content: center;
						align-items: center;
						position: absolute;
						left: 0;
						bottom: -23rpx;

						image {
							width: 28rpx;
							height: 28rpx;
						}

						text {
							color: #222322;
							font-size: 22rpx;
						}
					}
				}

				.user_view {
					flex: 1;
					padding: 14rpx 0 20rpx 20rpx;
					box-sizing: border-box;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.name {
						color: #FFFFFF;
						font-size: 36rpx;
						font-weight: bold;
					}

					.user_id {
						color: #FFFFFF;
						font-size: 28rpx;
					}
				}

				.interest {
					padding: 15rpx;
					box-sizing: border-box;
					display: flex;
					flex-direction: column;
					align-items: flex-end;
					justify-content: center;

					.label {
						margin: 8rpx 0;
						width: 160rpx;
						height: 40rpx;
						line-height: 40rpx;
						text-align: center;
						color: #111111;
						font-size: 28rpx;
						font-weight: bold;
						border-radius: 6rpx;
						background: url(@/static/images/m6.png) no-repeat;
						background-size: 100% 100%;
					}

					.bind_info {
						color: #FFFFFF;
						font-size: 28rpx;
						display: flex;
						align-items: center;

						view {
							white-space:nowrap;
							padding: 0 10rpx;
							box-sizing: border-box;
						}
					}
				}
			}

			.storeroom {
				padding: 52rpx 10rpx 0;
				box-sizing: border-box;

				.store_view {
					border-radius: 20rpx 20rpx 0 0;
					padding: 30rpx;
					box-sizing: border-box;
					background-color: #222;

					.store_head {
						height: 34rpx;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.left {
							display: flex;
							align-items: center;

							image {
								width: 34rpx;
								height: 34rpx;
							}

							text {
								color: #F3E4BC;
								font-size: 28rpx;
								font-weight: bold;
							}
						}

						.right {
							display: flex;
							align-items: center;

							text {
								color: #999999;
								font-size: 20rpx;
							}

							image {
								width: 24rpx;
								height: 24rpx;
							}
						}
					}

					.store_item {
						width: 100%;
						height: 72rpx;
						margin-top: 30rpx;
						padding: 0 20rpx;
						box-sizing: border-box;
						background: url(@/static/images/m6.png) no-repeat;
						background-size: 100% 100%;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.left {
							display: flex;
							overflow: hidden;

							image {
								width: 32rpx;
								height: 32rpx;
							}

							text {
								flex: 1;
								color: #111111;
								font-size: 32rpx;
								font-weight: bold;
								white-space: nowrap;
								text-overflow: ellipsis;
								overflow: hidden;
								padding-left: 5rpx;
								padding-right: 10rpx;
								box-sizing: border-box;
							}
						}

						.right {
							width: 200rpx;
							white-space: nowrap;
							display: flex;
							align-items: center;

							.key {
								color: #111111;
								font-size: 28rpx;
								font-weight: bold;
							}

							.value {
								color: #111111;
								font-size: 32rpx;
								font-weight: bold;
							}
						}
					}
				}
			}
		}

		.content {
			padding: 25rpx 22rpx 10rpx;
			box-sizing: border-box;

			.order_list {
				// height: 258rpx;
				border-radius: 20rpx;
				padding: 0 22rpx;
				box-sizing: border-box;
				background: #FFFFFF;

				.order_head {
					height: 82rpx;
					border-bottom: 1px solid #F2F2F2;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.key {
						color: #222222;
						font-size: 28rpx;
						font-weight: bold;
					}

					.value {
						display: flex;

						view {
							color: #666666;
							font-size: 20rpx;
						}

						image {
							width: 24rpx;
							height: 24rpx;
						}
					}
				}

				.order_cell {
					padding: 24rpx 0 50rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;

					.item {
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;

						image {
							width: 56rpx;
							height: 56rpx;
						}

						text {
							color: #222222;
							font-size: 24rpx;
							margin-top: 20rpx;
						}
					}
				}
			}

			.property {
				margin: 18rpx 0;
				height: 155rpx;
				border-radius: 20rpx;
				background: #FFFFFF;
				display: flex;
				justify-content: space-between;

				.item {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;

					&:last-child {
						position: relative;

						&::after {
							content: "";
							width: 2rpx;
							height: 30rpx;
							background: #EEEEEE;
							position: absolute;
							left: 0;
							top: 50%;
							transform: translateY(-50%);
							z-index: 2;
						}
					}

					.cell {
						height: 56rpx;
						line-height: 56rpx;

						image {
							width: 56rpx;
							height: 56rpx;
						}
					}

					.unit {
						color: #222222;
						font-size: 24rpx;
						margin-top: 16rpx;
					}
				}
			}

			.list {
				border-radius: 20rpx;
				background: #FFFFFF;
				overflow: hidden;

				.item {
					height: 100rpx;
					padding-left: 30rpx;
					padding-right: 12rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.left {
						display: flex;
						align-items: center;

						text {
							color: #333333;
							font-size: 28rpx;
							padding-left: 14rpx;
							box-sizing: border-box;
						}
					}

					image {
						width: 40rpx;
						height: 40rpx;
					}
				}
			}
		}
	}
</style>